//
//  CLsaveTool.m
//  I-want-go
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLsaveTool.h"
#import "CLpeopleViewController.h"
@implementation CLsaveTool

// 创建Movie文件夹
+ (void)isMake {
    
    // 1 找到沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *nameFile = [sandBoxPath stringByAppendingPathComponent:@"Disgner"];
    // 创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者方法 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:nameFile]) {
        [manager createDirectoryAtPath:nameFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+ (BOOL)ishaveNameInPlist:(CLguidang *)disgner {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Disgner/disgner.plist"];
    // 反归档
    NSArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    NSLog(@"%@", docPath);
//     遍历 是否有
    for (CLguidang *temp in nameArr) {
        if ([temp.title isEqualToString:disgner.title]) {
            NSLog(@"1");
            return NO;
        }
    }
    
    return YES;
}

// 保存收藏
+ (void)saveNameToPlist:(CLguidang *)disgner {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", sandBoxPath);
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Disgner/disgner.plist"];
    // 反归档
    NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    if (nameArr == nil) {
        nameArr = [NSMutableArray array];
    }
    
    [nameArr addObject:disgner];
    [NSKeyedArchiver archiveRootObject:nameArr toFile:docPath];
    NSLog(@"%@", docPath);
    
}

// 取消关注

+ (void)cancleNameInPlist:(CLguidang *)disgner {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Disgner/disgner.plist"];
    NSMutableArray *nameArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    for (CLguidang *temp in nameArr) {
        if ([temp.title isEqualToString:disgner.title]) {
            [nameArr removeObject:temp];
            break;
        }
    }
    
    //
    [NSKeyedArchiver archiveRootObject:nameArr toFile:docPath];
    
}












@end
